package com.rds.benchmark.connector;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class SingleJedisConnector implements JedisConnector {

    private final JedisPool pool;

    private String stringDes;

    public SingleJedisConnector(final String host, final int port, final int max_threads, final String pass) {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(max_threads + 1);
        poolConfig.setMaxIdle(max_threads + 1);

        pool = new JedisPool(poolConfig, host, port, null, pass);

        stringDes = "SingleJedisConnector(host='" + host + "', port='"+ port +"' )";
        System.out.println(stringDes + " connecting....");
    }

    @Override
    public boolean test() {
        try(Jedis jedis = pool.getResource()) {
            return "PONG".equalsIgnoreCase(jedis.ping());
        }
    }

    @Override
    public String type() {
        return null;
    }


    @Override
    public Jedis getJedis() {
        return pool.getResource();
    }

    @Override
    public String toString() {
        return stringDes;
    }
}
